package com.applet.system.controller;

import java.util.List;

import com.applet.code.util.Page;
import com.applet.system.entity.HttpResult;



import com.applet.system.utils.UserContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import com.applet.system.entity.Role;
import com.applet.system.entity.dto.RoleDto;
import com.applet.system.service.RoleService;

/**
 * 
 * <b>Description:角色/角色权限</b> 
 * 
 * <b>Author:</b> 1217460714@qq.com
 * <b>Date:</b> 2019年11月21日 下午1:53:30
 * <b>Copyright:</b> Copyright &copy;2018-2018 istp Co., Ltd. All rights reserved.
 * <b>Changelog:</b>
 *   Ver   Date                  Author              Detail
 *   ----------------------------------------------------------------------
 *   1.0   2019年11月21日
 *         new file.
 * </pre>
 */
@RestController
@RequestMapping("/system/role")
public class RoleController {

	@Autowired
	private RoleService roleServiceImpl;
	
	@GetMapping("/getPage")
	public HttpResult getPage(Page page, Role role) {
		page.initPagingData(Role.class);
		return new HttpResult(page.setResult(this.roleServiceImpl.getPage(role)));
	}
	
	@PostMapping("/delete")
	public HttpResult delete(@RequestParam("ids[]") List<Long> ids) {
		this.roleServiceImpl.delete(ids);
		return HttpResult.OK;
	}

	@PostMapping("/add")
	public HttpResult add(Role role) {
		String userName = UserContextUtil.get().getUserName();
		role.setCreater(role.getName());
		role.setEditor(userName);
		this.roleServiceImpl.add(role);
		return HttpResult.OK;
	}
	

	@PostMapping("/info")
	public HttpResult info(Long id) {
		return new HttpResult(this.roleServiceImpl.getRoleById(id));
	}
	
	@PostMapping("/update")
	public HttpResult update(Role role) {
		role.setEditor(UserContextUtil.get().getUserName());
		this.roleServiceImpl.update(role);
		return HttpResult.OK;
	}

	@PostMapping("/authorize")
	public HttpResult authorize(@RequestBody RoleDto role) {
		role.setCreater(UserContextUtil.get().getUserName());
		this.roleServiceImpl.authorize(role);
		return HttpResult.OK;
	}

	/**
	 * 获取角色拥有权限的菜单/动作id集合
	 * @param roleId
	 * @return
	 */
	@GetMapping("/getRoleAuth")
	public HttpResult getRoleAuth(Long roleId) {
		RoleDto role = new RoleDto();
		role.setId(roleId);
		role.setActionIds(this.roleServiceImpl.getActionIds(roleId));
		role.setMenuIds(this.roleServiceImpl.getMenuIds(roleId));
		return new HttpResult(role);
	}

	/**
	 * 获取角色拥有权限的菜单/动作id集合
	 * @param
	 * @return
	 */
	@GetMapping("/getAll")
	public HttpResult getAll() {
		return new HttpResult(this.roleServiceImpl.getAll());
	}
	
}
